package com.tulele.vtool.task.schedule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.tulele.vtool.model.ArticleInfo;
import com.tulele.vtool.model.combinedEntity.ArticleAudioInfo;
import com.tulele.vtool.model.combinedEntity.ArticleContentInfo;
import com.tulele.vtool.service.IArticleInfoService;
import com.tulele.vtool.utils.AliImgUtil;
import com.tulele.vtool.utils.DateUtil;
import com.tulele.vtool.utils.SysConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Component
@Slf4j
public class SpiderArticleScheduleTask {

    @Autowired
    private IArticleInfoService articleInfoService;

    //@Scheduled(cron="0 0/5 * * * ?")
    public void processSpiderFileUpload() {

        log.info("[enter processSpiderFileUpload]");

        List<ArticleInfo> articleInfos = articleInfoService.getToBeProcessedSpiderArticleInfo();

        if (CollectionUtils.isNotEmpty(articleInfos)) {

            articleInfos.stream().forEach(info -> {

                ArticleInfo articleInfo = new ArticleInfo();
                articleInfo.setId(info.getId());

                String thumbnailUrl = info.getArticleImgUrl();
                if (StringUtils.isNotBlank(thumbnailUrl)) {

                    String imgName = UUID.randomUUID().toString() + ".png";
                    AliImgUtil.uploadSpiderArticleThumbnail(info.getId(), thumbnailUrl, imgName);

                    String finalImgUrl = SysConstant.ARTICLE_IMG_ACCESS_PREFIX + SysConstant.PLATFORM_ARTICLE_THUMBNAIL_FOLDER_PATH + info.getId() + "/" + imgName;
                    articleInfo.setArticleImgUrl(finalImgUrl);

                }

                List<ArticleContentInfo> articleContentInfoList = JSONObject.parseArray(info.getArticleContent(), ArticleContentInfo.class);

                List<ArticleContentInfo> finalArticleContentInfoList = Lists.newArrayList();

                if (CollectionUtils.isNotEmpty(articleContentInfoList)) {

                    articleContentInfoList.forEach(contentInfo -> {

                        ArticleContentInfo articleContentInfo = new ArticleContentInfo();

                        //1-文本 2-图片 3-音频
                        int textType = Optional.ofNullable(contentInfo.getTextType()).orElse(0);

                        if (textType == 1) {

                            articleContentInfo.setTextType(1);
                            articleContentInfo.setTextContent(contentInfo.getTextContent());

                        } else if (textType == 2) {

                            articleContentInfo.setTextType(2);

                            String imgUrl = (String)contentInfo.getTextContent();
                            if (StringUtils.isNotBlank(imgUrl)) {

                                String imgName = UUID.randomUUID().toString() + ".png";
                                AliImgUtil.uploadSpiderArticleImg(info.getId(), imgUrl, imgName);

                                String finalImgUrl = SysConstant.ARTICLE_IMG_ACCESS_PREFIX + SysConstant.PLATFORM_ARTICLE_IMG_FOLDER_PATH + info.getId() + "/" + imgName;

                                articleContentInfo.setTextContent(finalImgUrl);

                            }

                        } else if (textType == 3) {

                            articleContentInfo.setTextType(3);

                            articleContentInfo.setTextTitle(contentInfo.getTextTitle());

                            //List<ArticleAudioInfo> articleAudioInfos = (List<ArticleAudioInfo>)contentInfo.getTextContent();

                            JSONArray jsonAry = (JSONArray)contentInfo.getTextContent();

                            List<ArticleAudioInfo> articleAudioInfos = jsonAry.toJavaList(ArticleAudioInfo.class);

                            List<ArticleAudioInfo> finalArticleAudioInfoList = Lists.newArrayList();
                            if (CollectionUtils.isNotEmpty(articleAudioInfos)) {

                                articleAudioInfos.forEach(audioInfo -> {

                                    ArticleAudioInfo articleAudioInfo = new ArticleAudioInfo();
                                    articleAudioInfo.setType(audioInfo.getType());

                                    String audioName = UUID.randomUUID().toString();
                                    AliImgUtil.uploadSpiderArticleAudio(info.getId(), audioInfo.getSource(), audioName);

                                    String finalAudioUrl = SysConstant.ARTICLE_IMG_ACCESS_PREFIX + SysConstant.PLATFORM_ARTICLE_AUDIO_FOLDER_PATH + info.getId() + "/" + audioName;
                                    articleAudioInfo.setSource(finalAudioUrl);

                                    finalArticleAudioInfoList.add(articleAudioInfo);

                                });

                                articleContentInfo.setTextContent(finalArticleAudioInfoList);

                            }

                        }

                        finalArticleContentInfoList.add(articleContentInfo);

                    });

                    articleInfo.setArticleContent(JSON.toJSONString(finalArticleContentInfoList));

                }

                articleInfo.setModifyTime(DateUtil.getCurDate());
                articleInfo.setSpiderProcessStatus(1);

                try {

                    articleInfoService.modifyUserArticleInfo(articleInfo);

                } catch (Exception e) {

                    log.error("[articleSourceSyncException]", e);

                    ArticleInfo errorArticle = new ArticleInfo();
                    errorArticle.setId(info.getId());
                    errorArticle.setSpiderProcessStatus(1);
                    errorArticle.setModifyTime(DateUtil.getCurDate());
                    articleInfoService.modifyUserArticleInfo(errorArticle);
                }


            });

        }

        log.info("[exit processSpiderFileUpload]");

    }

}
